package eSn.Core;

import java.lang.StringBuffer;
import java.util.ArrayList;
import java.util.List;

/***
 * 字符串处理类
 * @author Administrator
 *
 */
public class Str {
	//----------------------------------------------------------------------------
	//字符常量定义
	//----------------------------------------------------------------------------
	/**
	 * 回车符
	 */
	public static final char Cr = '\r';
	/**
	 * 换行符
	 */
	public static final char Lf = '\n';
	
	public static final String CrLf = "\r\n";

	
	//----------------------------------------------------------------------------
	//字符处理
	//----------------------------------------------------------------------------
	public static final String Copy(String obj,int length){
		if (length <= 0) return "";
		java.lang.StringBuffer sb = new java.lang.StringBuffer();
		for(int i = 0 ;i < length; i++) sb.append(obj);
		return sb.toString();
	}

	//----------------------------------------------------------------------------
	//字符子串处理
	//----------------------------------------------------------------------------
	/**
	 * 取得字符串长度(UniCode)
	 */
	public static final int Len(String obj){
		return obj.length();
	}
	/**
	 * 取得字符串长度(ASCII)
	 */	
	public static final int LenB(String obj){
		 if (obj.length() == 0 ) return 0;
		 int j = 0;
		 char[] a = obj.toCharArray();
		 for(int i = 0 ; i < obj.length();i++ ){
			 int k = (int)a[i];
			 if (k < 0 || k > 255 ){
				 j = j + 2;
			 }else{
				 j = j + 1;				 
			 }
		 }
		 return j;
	}
	/**
	 * 取得字符串左边的子串
	 */	
	public static final String Left(String obj,int length) throws Exception{
		try{
		return obj.substring(0, length);
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 取得字符串右边的子串
	 */	
	public static final String Right(String obj,int length) throws Exception{
		try{
		return obj.substring(obj.length()-length);
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 取得字符串子串
	 * @param obj 待处理的原串
	 * @param start 开始取的位置（0 至 length-1）
	 * @param length 取得开始位置后的长度过（0 至 length-start）
	 * @return 取得的子串
	 * @throws eSnException 标准系统异常
	 */
	public static final String Mid(String obj,int start, int length) throws  Exception{
		try{
		return obj.substring(start,start + length);
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 将一个字符串数组合并成字符串
	 * @param obj 待合并的字符串数组
	 * @param filter 分隔符
	 * @return 合并后的字符串
	 */
	public static final String Join(ArrayList<?> obj,Object filter){
		StringBuffer sb = new StringBuffer();
		for(int i = 0 ; i < obj.size();i++){
			if(obj.size()-1 == i){
				sb.append(String.valueOf(obj.get(i)));
			}else{
				sb.append(String.valueOf(obj.get(i))+ String.valueOf(filter));				
			}
		}
		return sb.toString();
	}
	//----------------------------------------------------------------------------
	//分隔与组合
	//----------------------------------------------------------------------------

	/**
	 * 将一个字符串按指定的分隔符转成数据
	 * @param obj 待分隔对象
	 * @param filter 分隔符
	 * @return 分隔后的数组
	 */
	public static final String[] Split(String obj,Object filter){
		return obj.split(String.valueOf(filter));
	}

	/**
	 * 将一个字符串折分成字符数组
	 * @param obj 待分隔对象
	 * @return 分隔后的数组
	 */
	public static final char[] Split(String obj){
		return obj.toCharArray();
	}
    /**
     * 对一个字符串反向生成另一字字符串
     * @param obj
     * @return
     */
	public static final String Reverse(String obj){
		char[] t = obj.toCharArray();
		StringBuffer sb = new StringBuffer();
		for(int i = 0;i<t.length;i++){
			sb.append(t[t.length -1 -i]);
		}
		return sb.toString();
	}
	
	//----------------------------------------------------------------------------
	//查找
	//----------------------------------------------------------------------------
	/**
	 * 从左边查找一个字符串在另一个字符串中所出现的位置 
	 * @param obj
	 * @param info 
	 * @param from 开始查找的位置，如没有，则为 0
	 * @return 如果没有找到为-1,范围是 0 至 length -1
	 */
	public static int Find(String obj,Object info,int from){
		return obj.indexOf(String.valueOf(info),from);
	}
 

	
	public static final  String Join(String[] value ,String partition){
		StringBuffer sb = new  StringBuffer();
		for(String s:value) sb.append(s + partition);
		String rt = sb.toString();
		return rt.substring(0, rt.length()-partition.length());
	}
	
	public static final String Join(List<String> value ,String partition){
		return Join(ListToArray(value),partition);
	}
	
	public static final String Replace(String src,String find,String repl){
		return src.replaceAll(find, repl);
	}
	
	public static final String[] Split(String src,String partition){
		return src.split(partition);
	}
	
	public static final List<String> ArrayToList(String[] src){
		List<String> v = new ArrayList<String>();
		for(String s : src) v.add(s);
		return v;
	}
	public static final String[] ListToArray(List<String> src){
		String[] v = new String[src.size()];
		int i = 0;
		for(String s: src){
			v[i] = s;
			i ++;
		}
		return v;
	}
	
}
